const express = require('express')
const router = express.Router()
const opera = require('../utils/dbOperation')
const result = require('../utils/response')
const time = require('../utils/timeUtils')
const db = require('../utils/db')
const uid = require('../utils/getUid')

router.delete('/delete', (req, res) => {
  const data = req.body
  new Promise((resolve, reject) => {
    opera.del('pro_fun_arg', { id: data.id }, (results) => {
      if (data.arg_type === 'vo') {
        opera.del('pro_fun_arg', { pid: data.id }, (back) => {
          resolve()
        })
      } else {
        resolve()
      }
    })
  }).then(() => res.send(result.success()))
})

router.post('/list', async (req, res) => {
  const data = req.body
  data.pid = 0
  const resData = []

  const funArgPro = (funData) => {
    return new Promise((resolve, reject) => {
      opera.select('pro_fun_arg', funData, (results) => resolve(results))
    })
  }

  const selectFunArg = (funId, pid) => {
    return new Promise((resolve, reject) => {
      opera.select('pro_fun_arg', { fun_id: funId, pid: pid }, (childrenBack) => resolve(childrenBack))
    })
  }

  const filePro = (argTypeId) => {
    return new Promise((resolve, reject) => {
      opera.findOne('pro_file', { id: argTypeId }, (fileBack) => resolve(fileBack))
    })
  }

  const fieldPro = (argTypeId) => {
    return new Promise((resolve, reject) => {
      opera.select('pro_field', { file_id: argTypeId }, (fieldBack) => resolve(fieldBack))
    })
  }

  const tableColumnPro = (superId) => {
    return new Promise((resolve, reject) => {
      opera.select('pro_table_column', { table_id: superId }, (columnBack) => resolve(columnBack))
    })
  }

  const funArgRes = await funArgPro(data)

  for (const item of funArgRes) {
    if (item.arg_type !== 'vo') {
      resData.push(item)
    }
    const selectFunArgRes = await selectFunArg(data.fun_id, item.id)
    const fileRes = await filePro(item.arg_type_id)
    const fieldRes = await fieldPro(item.arg_type_id)

    for (const fieldItem of fieldRes) {
      const arr = selectFunArgRes.filter(val => val.name === fieldItem.name)
      if (arr.length > 0) continue

      const obj = {}
      //obj.node_type = 'children'
      obj.arg_type = 'base'
      obj.name = fieldItem.name
      obj.fun_id = data.fun_id
      obj.collection_type = fieldItem.collection_type
      obj.file_id = fieldItem.id
      obj.pid = item.id
      obj.file_type = 'vo'
      obj.comment = fieldItem.comment
      obj.arg_type_name = fieldItem.type_path
      selectFunArgRes.push(obj)
    }

    const superId = fileRes.super_id
    if (superId) {
      const columnRes = await tableColumnPro(superId)
      for (const columnItem of columnRes) {
        const arr = selectFunArgRes.filter(val => val.name === columnItem.name)
        if (arr.length > 0) continue
        const obj = {}
        //obj.node_type = 'children'
        obj.arg_type = 'base'
        obj.name = columnItem.name
        obj.fun_id = data.fun_id
        obj.collection_type = 'single'
        obj.file_id = columnItem.id
        obj.pid = item.id
        obj.file_type = 'table'
        obj.comment = columnItem.comment
        obj.arg_type_name = columnItem.mysql_type
        selectFunArgRes.push(obj)
      }
      item.children = selectFunArgRes
      resData.push(item)

    } else {
      item.children = selectFunArgRes
      resData.push(item)
    }
  }
  res.send(result.success(resData))
})


router.post('/add', (req, res) => {
  const data = req.body
  data.id = uid();
  data.update_time = time.getNowFormatDate()
  data.create_time = time.getNowFormatDate()

  if (data.module_id == '0') {
    delete data.module_id
  }

  opera.insert('pro_fun_arg', data, (results) => {
    res.send(result.success(data))
  })
})

router.post('/detail', (req, res) => {
  const data = req.body
  opera.findOne('pro_fun_arg', data, (back) => {
    res.send(result.success(back))
  })
})

router.put('/update', (req, res) => {
  const data = req.body
  const id = data.id
  delete data.id
  data.update_time = time.getNowFormatDate()
  opera.update('pro_fun_arg', data, { id: id }, (back) => {
    res.send(result.success())
  })
})

router.get('/validList', (req, res) => {
  db.query('select * from pro_validate', [], (back) => {
    res.send(result.success(back))
  })
})

module.exports = router
